//index.js
const app = getApp()
import {geocoder,getWeather} from '../../lib/api'
Page({
  data: {
    // 页面数据
    backgroundImage: '../../images/cloud.jpg',
    backgroundColor: '#62aadc',
    "air": {
      "status": 0,
      "aqi": "77",
      "color": "#00cf9a",
      "name": "良"
    },
    "current": {
      "backgroundImage": "https://tianqi-1d3bf9.tcb.qcloud.la/bg/day/overcast.jpg",
      "backgroundColor": "#5c7a93",
      "temp": "35",
      "wind": "南风",
      "windLevel": "1",
      "weather": "阴",
      "humidity": "73",
      "icon": "yin",
      "ts": "2018-08-12 14:54"
    },
    "tom":{
      
    },
    "after_tom":{

    },
    "tips":"啦啦啦啦啦啦啦",
    "lat":"",
    "lon":"",
    "address":"",
    "province":"",
    "city":"",
    "district":"",
    // 24小时天气数据

    // 一周天气数据

    // 生活指数
  },
  onLoad:function(){
    // 获取当前页面对象
    // url是否携带 地址 参数(分享)
    const pages = getCurrentPages();
    const currentPage = pages[pages.length-1];
    console.log(currentPage.optios);
    const query = currentPage.options;
    if(query&&query.address&&query.lat&&query.lon){
      // 存在
    }else{
      // 获取storage缓存数据
      this.setDataFromCache();
      // 重新定位
      this.getLocation();
    }
    
  },
  getLocation(){
    wx.getLocation({
      type: 'gcj02',
      // 箭头函数this 不改变
      success: (res) =>{
        this.updateLocation(res);
      },
      fail: ()=> {
        // 打开位置权限
        this.openLocation();
      },
    })
  },
  // 更新位置
  updateLocation(res){
    console.log(res);
    let {
      latitude:lat,
      longitude:lon,
      name
    } = res;
    // 什么业务?
    let data = {
      lat,
      lon
    }
    if(name){
      data.address = name;
    }
    this.setData(data);
    // 根据 经纬度 拿到地址名称
    this.getAddress(lat,lon,name);
  },
  getAddress(lat,lon,name){
    wx.showLoading({
      title: '定位中',
      mask:true
    });
    let fail = e=>{
      this.setData({
        address:name || '郑州市郑东新区绿地新都会'
      });
      wx.hideLoading();
      this.getWeatherData();
    };
    // 根据经纬度 获取地址信息
    // 腾讯地图
    geocoder(
      lat,
      lon,
      (res) => {
        console.log(res)
        wx.hideLoading();
        let result = res.data.result;
        if(res.statusCode===200&&result&&result.address){
          let {
            address,
            formatted_addresses,
            address_component
          } = result;
          if(formatted_addresses && (formatted_addresses.recommend || formatted_addresses.rough)){
            address = formatted_addresses.recommend || formatted_addresses.rough;
          }
          let {
            province,
            city,
            district
            // district:country
            // 区县
          } = address_component;
          this.setData({
            province,
            city,
            district,
            address: name || address
          })
          this.getWeatherData();
        }else{
          fail();
        }
      },
      fail
    )   
  },
  // 提示用户开启 位置权限
  openLocation(){
    wx.showToast({
      title: '检测到您未授权使用位置权限,请先开启哦',
      icon:'none',
      duration:'3000'
    })
  },
  setDataFromCache(){

  },
  // 获取天气情况
  getWeatherData(){
    wx.showLoading({
      title: '获取数据中',
      mask:true
    });
    // 失败回调
    const fail = e =>{
      wx.hideLoading();
      wx.showToast({
        title: '加载失败,请稍后再试',
        icon:'none',
        duration:3000
      })
    };
    let {
      province,
      lat,
      lon,
      city,
      district
    } = this.data;
    // 使用云函数功能 promise
    // getWeather(lat,lon).then(res=>{
    //   wx.hideLoading();
    // })
    getWeather(
      lat,
      lon,
      (res)=>{
        console.log(res);
        wx.hideLoading();
        if(res.data){
          let data = res.data.HeWeather6[0];
          this.render(data);
        }
      },
      fail
    )
  },
  // 现实渲染天气
  render(data){
    console.log(data);
    // 设置数据
    const {
      wind_dir:wind,
      wind_sc:windLevel,
      tmp:temp,
      cond_txt:weather,
      hum:humidity
    } = data.now;
    const current = {
      wind,
      windLevel,
      weather,
      temp,
      humidity
    };
    // const {
    //   tmp_min,
    //   tmp_max,
    //   cond_txt_d:cond_d,
    //   cond_txt_n: cond_n
    // } = data.daily_forecast[1];
    // const tom = {
    //   tmp_min,
    //   tmp_max,
    //   cond_d,
    //   cond_n
    // }
    const tom = data.daily_forecast[1];
    const after_tom = data.daily_forecast[2];
    this.setData({
      current,
      tom,
      after_tom
    })
  }
})
